package com.wu.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class EnumUtils {

    public static <T extends Enum<T>> List<String> getEnumCodes(Class<T> enumClass) {
        List<String> codes = new ArrayList<>();

        for (T enumConstant : enumClass.getEnumConstants()) {
            try {
                Field codeField = enumClass.getDeclaredField("code");
                codeField.setAccessible(true);
                Object codeValue = codeField.get(enumConstant);
                if (codeValue != null) {
                    codes.add(codeValue.toString());
                }
            } catch (NoSuchFieldException | IllegalAccessException e) {
                // 如果枚举没有 code 字段，或者访问出错，则跳过
                continue;
            }
        }
        return codes;
    }
}